package com.wwh.furns.dao.impl;

import com.wwh.furns.dao.BasicDAO;
import com.wwh.furns.dao.FurnDAO;
import com.wwh.furns.entity.Furn;

import java.util.List;

/**
 * @author Albert
 * @date 2024/5/27-14:29
 * @describe
 */
public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {
    @Override
    public List<Furn> queryFurns() {
        String sql = "SELECT `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` imgPath FROM furn";
        return queryMulti(sql, Furn.class);
    }

    @Override
    public int addFurn(Furn furn) {
        String sql = "INSERT INTO `furn`(`name`, `maker`, `price`, `sales`, `stock`, `img_path`) VALUES(?, ?, ?, ?, ?, ?)";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());
    }

    @Override
    public int deleteFurnById(int id) {
        String sql = "DELETE FROM `furn` WHERE id = ?;";
        return update(sql, id);
    }

    @Override
    public List<Furn> getPageItems(int begin, int pageSize) {
        String sql = "SELECT `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` imgPath FROM furn LIMIT ?, ?";
        return queryMulti(sql, Furn.class, begin, pageSize);
    }

    @Override
    public List<Furn> getPageItemsByName(int begin, int pageSize, String name) {
        String sql = "SELECT `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` imgPath FROM furn WHERE `name` LIKE ? LIMIT ?, ?";
        return queryMulti(sql, Furn.class, "%" + name + "%", begin, pageSize);
    }

    @Override
    public int getTotalRow() {
        String sql = "SELECT count(*) FROM `furn`;";
        return ((Number)queryScalar(sql)).intValue();
    }

    @Override
    public int getTotalRowByName(String name) {
        String sql = "SELECT count(*) FROM `furn` WHERE `name` LIKE ?";
        return ((Number)queryScalar(sql, "%" + name + "%")).intValue();
    }

    @Override
    public Furn queryFurnById(int id) {
        String sql = "SELECT  `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` imgPath  FROM `furn` WHERE id = ?";
        return querySingle(sql, Furn.class, id);
    }

    @Override
    public int updateFurn(Furn furn) {
        String sql = "UPDATE `furn` " +
                "SET `name` = ?, `maker` = ?, `price` = ?, `sales` = ?, `stock` = ? , `img_path` = ? " +
                "WHERE `id` = ?";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(),
                furn.getSales(), furn.getStock(), furn.getImgPath(), furn.getId());
    }
}
